﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Data;
    using System.IO;
    using System.Windows;
    using System.Windows.Controls;

    /// <summary>
    /// Interaction logic for ReportWindow.xaml
    /// </summary>
    public partial class ReportWindow : Window
        {

        public ReportWindow()
            {
            InitializeComponent();
            this.ExportFileControl.DialogFilter = FileFilters.Csv;
            var today = DateTime.Now;
            this.StartDatePicker.SelectedDate = new DateTime(today.Year, today.Month, 1);
            this.EndDatePicker.SelectedDate = new DateTime(today.Year, today.Month, DateTime.DaysInMonth(today.Year, today.Month));
            }

        private void Window_Loaded(object sender, RoutedEventArgs e)
            {
            if (!State.Settings.EnableReporting)
                {
                Utilities.ShowInformationMessage("Reporting",
                                                 "Song usage is currently not being recorded, therefore any reports generated will be inaccurate.", 
                                                 "To report on future song usage, please enable logging via the PowerSong Configuration dialog box, accessible via the Tools menu of the main form.");
                }
            }

        private void GenerateReportButton_Click(object sender, RoutedEventArgs e)
            {

            ReportGrid.Visibility = Visibility.Hidden;
            RawReportTextBox.Visibility = Visibility.Hidden;
            if (RawDataRadioButton.IsChecked == true)
                {

                RawReportTextBox.Visibility = Visibility.Visible;
                var task = new RawReport(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value.AddDays(1));
                task.Execute();
                RawReportTextBox.Text = task.ReportContents;

                }
            else if (SongUsageRadioButton.IsChecked == true)
                {

                ReportGrid.Visibility = Visibility.Visible;
                var task = new SongUsageReport(StartDatePicker.SelectedDate.Value, EndDatePicker.SelectedDate.Value.AddDays(1));
                task.Execute();
                ReportGrid.ItemsSource = task.Report.DefaultView;

                }
            
            }

        private void ExportButton_Click(object sender, RoutedEventArgs e)
            {

            if (ReportGrid.Visibility == Visibility.Visible)
                {

                try
                    {

                    // Ensure a file name has been specified
                    if (string.IsNullOrEmpty(ExportFileControl.FileName))
                        {
                        throw new Exception("Please enter a file name to write the data to.");
                        }

                    // Create the file
                    using (var file = new StreamWriter(new FileStream(ExportFileControl.FileName, FileMode.Create)))
                        {

                        // Export the columns
                        var firstLine = string.Empty;
                        foreach (DataGridTextColumn column in ReportGrid.Columns)
                            {
                            if (!string.IsNullOrEmpty(firstLine)) firstLine += ",";
                            firstLine += "\"" + column.Header.ToString().Replace('"', '\'') + "\"";
                            }
                        file.WriteLine(firstLine);

                        // Export the data
                        foreach (DataRowView row in ReportGrid.Items)
                            {
                            var line = string.Empty;
                            foreach (var item in row.Row.ItemArray)
                                {
                                if (!string.IsNullOrEmpty(line)) line += ",";
                                line += "\"" + item.ToString().Replace('"', '\'') + "\"";
                                }
                            file.WriteLine(line);
                            }

                        // Done
                        Utilities.ShowInformationMessage("Export Complete", "The data has been exported to the file '" + ExportFileControl.FileName + "'.");

                        }

                    }
                catch (Exception ex)
                    {
                    Utilities.ShowErrorMessage("Export Failure", ex.Message);
                    }

                }
            else
                {
                Utilities.ShowErrorMessage("Cannot Export Data", "It is not possible to export the type of data that you are looking at.");
                }

            }

        }

    }
